package com.vallis.asfc.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.vallis.asfc.entity.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author vallis
 * @version 1.0
 * @date 2020/6/29 13:03
 */
@Data
public class JwtUtils {
    //私钥
    static private String key="vallis";
    //失效时间
     static  private Long ttl=30*60*1000l;

    /**
     * 设置认证token
     */

    public static String createdToken(User user, Map<String,Object> map){

        long now =System.currentTimeMillis();//当前时间毫秒
        long exp=now +ttl;
        JwtBuilder builder = Jwts.builder().setId(user.getUserId().toString())
                .setSubject(user.getUserName())
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256,key)
              .setExpiration(new Date(exp));

        String token=builder.compact();
        return token;

    }
/**
 * 解析token
 */
public static  Claims parseToken(String token){
    Claims body = Jwts.parser().setSigningKey(key)
            .parseClaimsJws(token).getBody();

    return body;
}


}
